from django.shortcuts import redirect, render
from django.utils.deprecation import MiddlewareMixin


# 用于拦截为登入用户直接访问网站内容
class CheckLogin(MiddlewareMixin):
    white_list = ['/login', '/login/']  # 白名单

    def process_request(self, request):
        request_url = request.path_info  # 获取请求路径
        if request_url in CheckLogin.white_list or request.session.get('user'):
            return None
        else:
            return render(request, 'login.html')

    def process_response(self, request, response):

        return response
